<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
	<head>
	<meta charset="UTF-8">	
	<meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
	
	<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="../css/font.css">
	<link rel="stylesheet" href="../css/xadmin.css">
	<link rel="stylesheet" href="../layui/css/layui.css" media="all">
	<link rel="stylesheet" href="../css/amazeui.min.css"/>
	
    <script src="../scripts/jquery.min.js"></script>
    <script src="../layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
	<title>学生成绩列表</title>
	</head>
	
	<body>

    <input type="hidden" id="flag_manager" value=<%=session.getAttribute("flag_manager")%> />


    <div class="x-body">
      <div class="layui-row">
        <div class="layui-form layui-col-md12 x-so"  id="searchByInfo"  >
          <input class="layui-input" placeholder="用户名" name="user_name_input" id="user_name_input">
          <button class="layui-btn layui-btn-warm" onclick="searchByInfo()"><i class="layui-icon">&#xe615;</i>
          </button>
        </div>
      </div>
      <xblock>
        <button class="layui-btn layui-btn-danger" onclick="user_delete_many()"><i class="layui-icon"></i>批量删除</button>
          <a href="">
        <span class="x-right" style="line-height:40px">
        <button class="layui-btn"><i class="layui-icon">&#xe65c;</i>刷新</button>
        </span>
        </a>
      </xblock>
      <table class="layui-table">
        <thead>
          <tr>
            <th>
              <div id="-1" class="layui-unselect header layui-form-checkbox" lay-skin="primary">
                  <i class="layui-icon">&#xe605;</i>
              </div>
            </th>
            <th>用户名</th>
            <th>密码</th>
            <th>头像</th>
            <th>昵称</th>
            <th>性别</th>
            <th>操作</th>
        </thead>
        <tbody id="tbody_data_id">
        </tbody>
      </table>
      
   <!--page begin-->
   <div class="am-cf">
       共<span id="total_rows">1</span>条记录, 每页 <select id="page_nums" name="page_every" style="width: 70px;height: 20px;" onblur="select_blur()">
       <option selected="selected" value="5">05</option>
       <option value="10">10</option>
       <option value="15">15</option>
   </select> 条, 当前为  <input id="current_page" type="number" value="1" min="1" max="10" style="width: 50px;text-align: center" onblur="input_blur()" /> 页，
       共 <span id="total_pages"></span> 页
   <div class="am-fr">
    <ul class="am-pagination">
      <li><a href="javascript:;" onclick="firstPage()" >首页</a></li>
      <li><a href="javascript:;" onclick="prevPage()" >上一页</a></li>
      <li><a href="javascript:;" onclick="nextPage()" >下一页</a></li>
      <li><a href="javascript:;" onclick="lastPage()" >尾页</a></li>
    </ul>
  </div>
  </div>
  <!--page end-->
  </div>
 
 <!--用户操作使用form表单提交信息-->
 <form action="" id="sendOrder" name="sendOrder" method="post">
 </form> 
 <!--设置用来接受弹出窗口的整个form表单-->
 <div id="mianFromDiv" style="display:none;">
 </div>
 <!--弹框页面操作 end-->
<script>
//layui自启动

$.ajax({
    url:"${pageContext.request.contextPath}/page_split.do",
    data:{
        "start_row":"0",
        "page_nums":$("#page_nums").val()
    },
    success:function (data,status){
        if(status=="success"){
            var total_rows=data[data.length-1].user_id;
            var total_pages=Math.ceil(total_rows/$("#page_nums").val());
            $("#current_page").val(1);
            $("#total_rows").text(total_rows);
            $("#total_pages").text(total_pages);
            $("#current_page").attr("max",total_pages);
            changeRows(data);
        }
    }
});

layui.use('layer', function(){
  $('.demo').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});

function user_delete_many(){
    layer.confirm('确定删除？', {btn: ['确定','取消']},
        function(){
        var user_ids=[];
        var all_selected=$(".layui-form-checked");
        if(all_selected.length==0){
            layer.msg('您还没有选中任何选项');
            return ;
        }else{
            if(all_selected.length==1)
                if(all_selected[0].id==-1) {
                    layer.msg('您还没有选中任何选项');
                    return ;
                }
        }
        for(var i=0;i<all_selected.length;i++){
              user_ids.push(all_selected[i].id);
        }
        if($("#flag_manager").val()!="null"){
            $.ajax({
                url:"${pageContext.request.contextPath}/delete_user_many.do",
                dataType:"json",
                contentType:"application/json",
                data:{
                    "user_ids":JSON.stringify(user_ids),
                    "start_row":$("#current_page").val()*$("#page_nums").val()-$("#page_nums").val(),
                    "page_nums":$("#page_nums").val()
                },
                success:function(data,status){
                    if(status=="success"){
                        if($("#user_name_input").val()=="")
                          changeRows(data);
                        else
                            $("#tbody_data_id").empty();
                    }
                }
            });
            layer.msg('选中数据已被删除', {icon: 1});
        }else{
            layer.msg("操作失败，您不是管理员");
        }
        }, function(){

            layer.msg('取消删除', {});
        });
}
//用户自定义相关函数
function user_delete(user_id){
	layer.confirm('确定删除？', {btn: ['确定','取消']},
	 function(){
	    if($("#flag_manager").val()!="null"){
	    $.ajax({
            url:"${pageContext.request.contextPath}/delete_user.do",
            data:{
                "user_id":user_id,
                "start_row":$("#current_page").val()*$("#page_nums").val()-$("#page_nums").val(),
                "page_nums":$("#page_nums").val()
            },
            success:function(data,status){
                if(status=="success"){
                    if($("#user_name_input").val()=="")
                        changeRows(data);
                    else
                        $("#tbody_data_id").empty();
                }
            }
        });
        layer.msg('已经删除', {icon: 1});
        }else{
	        layer.msg("操作失败，您不是管理员");
        }
      }, function(){

        layer.msg('取消删除', {});
      });
}
//信息搜索
function searchByInfo(){
    var user_name=$("#user_name_input").val();
    if(user_name=="")
      layer.msg('至少填写一个查询信息', {icon: 0.5});
    else{
    $.ajax({
        url:"${pageContext.request.contextPath}/select_userByUser_name.do",
        data:{
            "user_name":user_name
        },
        success:function(data,status){
            if(status=="success"){
                if(data.user_id>0) {
                    $("#total_pages").text("1");
                    $("#total_rows").text("1");
                    $("#current_page").val("1").attr("max","1");
                    $("#tbody_data_id").empty();
                    var tr = $("<tr></tr>");
                    var div_head = $("<div></div>").attr({
                        "id": data.user_id,
                        "class": "layui-unselect layui-form-checkbox",
                        "lay-skin": "primary",
                        "onclick": "checkBox_click(" + data.user_id + ")",
                    });
                    var i1 = $("<i></i>").attr("class", "layui-icon").html("&#xe605;");
                    div_head.append(i1);
                    var td1 = $("<td></td>");
                    td1.append(div_head);
                    tr.append(td1);
                    var td_tool1 = $("<td></td>").text(data.user_name);
                    var td_tool2 = $("<td></td>").text(data.password);
                    var td_tool3 = $("<td></td>").text(data.user_img);
                    var td_tool4 = $("<td></td>").text(data.user_nickname);
                    var td_tool5 = $("<td></td>").text(data.sex);
                    tr.append(td_tool1, td_tool2, td_tool3, td_tool4, td_tool5);

                    var td2 = $("<td></td>").attr("class", "td-manage");
                    var a2 = $("<a></a>").attr({
                        "title": "删除",
                        "href": "javascript:user_delete(" + data.user_id + ");"
                    });
                    var i2 = $("<i></i>").attr("class", "layui-icon").html("&#xe640;");
                    a2.append(i2);
                    td2.append(a2);
                    tr.append(td2);
                    $("#tbody_data_id").append(tr);
                }else{
                    layer.msg("不存在该用户");
                }
            }
        }
    });
    }
}


//--------------------mysql数据库分页显示操作---------------------------------
//首页

function firstPage(){

    if($("#user_name_input").val()==""){
    $.ajax({
        url:"${pageContext.request.contextPath}/page_split.do",
        data:{
            "start_row":"0",
            "page_nums":$("#page_nums").val()
        },
        success:function (data,status){
            if(status=="success"){
                $("#current_page").val(1);
                changeRows(data);
            }
        }
    });
    }
}
function nextPage(){
    if($("#current_page").val()>=$("#total_pages").text()){
        layer.msg("这已经是最后一页了");
    }else{
    $.ajax({
        url:"${pageContext.request.contextPath}/page_split.do",
        data:{
            "start_row":$("#current_page").val()*$("#page_nums").val(),
            "page_nums":$("#page_nums").val()
        },
        success:function (data,status){
            if(status=="success"){
                var new_current_page=$("#current_page").val();
                new_current_page++;
                $("#current_page").val(new_current_page);
                changeRows(data);
            }
        }
    });
    }
}
function prevPage(){
    if($("#current_page").val()<=1){
        layer.msg("这已经是第一页了");
    }else{
    $.ajax({
        url:"${pageContext.request.contextPath}/page_split.do",
        data:{
            "start_row":$("#current_page").val()*$("#page_nums").val()-$("#page_nums").val()*2,
            "page_nums":$("#page_nums").val()
        },
        success:function (data,status){
            if(status=="success"){
                var new_current_page=$("#current_page").val();
                new_current_page--;
                $("#current_page").val(new_current_page);
                changeRows(data);
            }
        }
    });
    }
}
function lastPage(){
    if($("#user_name_input").val()==""){
    $.ajax({
        url:"${pageContext.request.contextPath}/page_split.do",
        data:{
            "start_row":$("#total_pages").text()*$("#page_nums").val()-$("#page_nums").val(),
            "page_nums":$("#page_nums").val()
        },
        success:function (data,status){
            if(status=="success"){
                $("#current_page").val($("#total_pages").text());
                changeRows(data);
            }
        }
    });
    }
}
function changeRows(data){
    $("#tbody_data_id").empty();
    for(var i=0;i<data.length-1;i++){
        var tr=$("<tr></tr>");
        var div_head=$("<div></div>").attr({
            "id":data[i].user_id,
            "class":"layui-unselect layui-form-checkbox",
            "lay-skin":"primary",
            "onclick":"checkBox_click("+data[i].user_id+")",
        });
        var i1=$("<i></i>").attr("class","layui-icon").html("&#xe605;");
        div_head.append(i1);
        var td1=$("<td></td>");
        td1.append(div_head);
        tr.append(td1);
        var td_tool1=$("<td></td>").text(data[i].user_name);
        var password_hide="";
        for(var jj=0;jj<data[i].password.length;jj++){
            password_hide+="*";
        }
        var td_tool2=$("<td></td>").text(password_hide);
        var td_tool3=$("<td></td>").text(data[i].user_img);
        var td_tool4=$("<td></td>").text(data[i].user_nickname);
        var td_tool5=$("<td></td>").text(data[i].sex);
        tr.append(td_tool1,td_tool2,td_tool3,td_tool4,td_tool5);


        var td2=$("<td></td>").attr("class","td-manage");
        // var a1=$("<a></a>").attr({
        //     "title":"编辑",
        //     "href":"javascript:;"
        // });
        // var i1=$("<i></i>").attr("class","layui-icon").html("&#xe642;");
        // a1.append(i1);

        var a2=$("<a></a>").attr({
            "title":"删除",
            "href":"javascript:user_delete("+data[i].user_id+");"
        });
        var i2=$("<i></i>").attr("class","layui-icon").html("&#xe640;");
        a2.append(i2);
        td2.append(a2);
        tr.append(td2);
        // new_div.append(tr);
        $("#tbody_data_id").append(tr);
    }
}

function checkBox_click(i){
    var id="#"+i;
    if($(id).hasClass('layui-form-checked')){
        $(id).removeClass('layui-form-checked');
    }else{
        $(id).addClass('layui-form-checked');
    }
}
function select_blur(){
    if($("#user_name_input").val()==""){
    var total_rows=$("#total_rows").text();
    var total_pages=Math.ceil(total_rows/$("#page_nums").val());
    $("#total_pages").text(total_pages);
    $("#current_page").attr("max",total_pages);
    var current_page=$("#current_page").val();
    if(current_page>total_pages){
        current_page=total_pages;
        $("#current_page").val(total_pages);
    }
    $.ajax({
        url:"${pageContext.request.contextPath}/page_split.do",
        data:{
            "start_row":current_page*$("#page_nums").val()-$("#page_nums").val(),
            "page_nums":$("#page_nums").val()
        },
        success:function (data,status){
            if(status=="success"){
                changeRows(data);
            }
        }
    });
    }

}
function input_blur(){

    var total_pages=parseInt($("#total_pages").text());
    var current_page=parseInt($("#current_page").val());
    if(current_page>total_pages||current_page<1){
        layer.msg("输入的页数不对");
    }else{
        if($("#user_name_input").val()==""){
    $.ajax({
        url:"${pageContext.request.contextPath}/page_split.do",
        data:{
            "start_row":current_page*$("#page_nums").val()-$("#page_nums").val(),
            "page_nums":$("#page_nums").val()
        },
        success:function (data,status){
            if(status=="success"){
                changeRows(data);
            }
        }
    });
        }
    }
}
</script>	
</body>
</html>
